package jnpf.controller;


import cn.hutool.core.util.StrUtil;

import io.swagger.annotations.ApiOperation;
import jnpf.common.enums.dify.DifyPermissionEnum;
import jnpf.model.dify.file.DocumentBase;
import jnpf.model.dify.file.req.CreateDocumentReq;
import jnpf.model.dify.file.req.CreateSegmentsReq;
import jnpf.model.dify.file.req.UpdateSegmentReq;
import jnpf.model.dify.know.req.CreateDataSetsReq;
import jnpf.service.DifyKnowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/dify/datasets")
public class DifyKnowController {
    @Autowired
    private DifyKnowService difyKnowService;

    @GetMapping("/list")
    @ApiOperation(value = "获取知识库列表", notes = "获取知识库列表")
    public ResponseEntity<String> getDatasetList() {
        try {
            String result = difyKnowService.getDatasetList();
            return new ResponseEntity<>(result, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>("获取知识库列表失败：" + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @ApiOperation(value = "创建空知识库", notes = "创建空知识库")
    @PostMapping("/createEmpty")
    public ResponseEntity<String> createEmptyDataset(@RequestBody CreateDataSetsReq createDataSetsReq) {
        if (StrUtil.isEmpty(createDataSetsReq.getName())) {
            return new ResponseEntity<>("创建空知识库失败：知识库名称参数为空", HttpStatus.BAD_REQUEST);
        }
        if (!DifyPermissionEnum.getKeyList().contains(createDataSetsReq.getPermission())) {
            return new ResponseEntity<>("创建空知识库失败：知识库权限参数错误", HttpStatus.BAD_REQUEST);
        }
        try {
            String result = difyKnowService.createEmptyDataset(createDataSetsReq);
            return new ResponseEntity<>(result, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>("创建空知识库失败：" + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping("/document/createByText")
    @ApiOperation(value = "在知识库中通过文本创建文档", notes = "在知识库中通过文本创建文档")
    public ResponseEntity<String> createDocumentByText(@RequestParam("datasetId") String datasetId, @RequestBody CreateDocumentReq params) {
        try {
            String result = difyKnowService.createDocumentByText(datasetId, params);
            return new ResponseEntity<>(result, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>("创建文档失败：" + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping("/documents/updateByText")
    @ApiOperation(value = "通过文本更新文档", notes = "通过文本更新文档")
    public ResponseEntity<String> updateDocumentByText(@RequestParam("datasetId") String datasetId,
                                                       @RequestParam("documentId") String documentId,
                                                       @RequestBody CreateDocumentReq params) {
        try {
            String result = difyKnowService.updateDocumentByText(datasetId, documentId, params);
            return new ResponseEntity<>(result, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>("通过文本更新文档失败：" + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @GetMapping("/documents/list")
    @ApiOperation(value = "获取知识库文档列表", notes = "获取知识库文档列表")
    public ResponseEntity<String> getDatasetDocumentList(@RequestParam("datasetId") String datasetId) {
        try {
            String result = difyKnowService.getDatasetDocumentList(datasetId);
            return new ResponseEntity<>(result, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>("获取知识库文档列表失败：" + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping("/documents/segments/query")
    @ApiOperation(value = "查询文档分段", notes = "查询文档分段")
    public ResponseEntity<String> queryDocumentSegment(@RequestBody DocumentBase documentBase) {
        try {
            String result = difyKnowService.queryDocumentSegment(documentBase.getDatasetId(), documentBase.getDocumentId());
            return new ResponseEntity<>(result, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>("查询文档分段失败：" + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping("/documents/segments/add")
    @ApiOperation(value = "新增分段", notes = "新增分段")
    public ResponseEntity<String> addDocumentSegment(@RequestParam("datasetId") String datasetId,
                                                     @RequestParam("documentId") String documentId,
                                                     @RequestBody CreateSegmentsReq createSegmentsReq) {
        try {
            String result = difyKnowService.addDocumentSegment(datasetId, documentId, createSegmentsReq);
            return new ResponseEntity<>(result, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>("新增分段失败：" + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping("/documents/segments/update")
    @ApiOperation(value = "更新文档分段", notes = "更新文档分段")
    public ResponseEntity<String> updateDocumentSegment(@RequestParam("datasetId") String datasetId,
                                                        @RequestParam("documentId") String documentId,
                                                        @RequestParam("segmentId") String segmentId,
                                                        @RequestBody UpdateSegmentReq params) {
        try {
            String result = difyKnowService.updateDocumentSegment(datasetId, documentId, segmentId, params);
            return new ResponseEntity<>(result, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>("更新文档分段失败：" + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }


    @DeleteMapping("/documents/segments/delete")
    @ApiOperation(value = "删除文档分段", notes = "删除文档分段")
    public ResponseEntity<String> deleteDocumentSegment(@RequestParam("datasetId") String datasetId,
                                                        @RequestParam("documentId") String documentId,
                                                        @RequestParam("segmentId") String segmentId) {
        try {
            String result = difyKnowService.deleteDocumentSegment(datasetId, documentId, segmentId);
            return new ResponseEntity<>(result, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>("删除文档分段失败：" + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @DeleteMapping("/documents")
    @ApiOperation(value = "删除文档", notes = "删除文档")
    public ResponseEntity<String> deleteDocument(@RequestBody DocumentBase params) {
        try {
            String result = difyKnowService.deleteDocument(params.getDatasetId(), params.getDocumentId());
            return new ResponseEntity<>(result, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>("删除文档失败：" + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @DeleteMapping("/{datasetId}")
    @ApiOperation(value = "删除知识库", notes = "删除知识库")
    public ResponseEntity<String> deleteDataset(@PathVariable("datasetId") String datasetId) {
        try {
            String result = difyKnowService.deleteDataset(datasetId);
            return new ResponseEntity<>(result, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>("删除知识库失败：" + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }


    //用不到
    @Deprecated
    @GetMapping("/documents/{documentId}/indexing-status")
    @ApiOperation(value = "获取文档嵌入状态（进度）", notes = "获取文档嵌入状态（进度）")
    public ResponseEntity<String> getDocumentIndexingStatus(@PathVariable("documentId") String documentId,
                                                            @RequestParam("batch") String batch) {
        try {
            String result = difyKnowService.getDocumentIndexingStatus(documentId, batch);
            return new ResponseEntity<>(result, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>("获取文档嵌入状态失败：" + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

//TODO 通过文件创建先搁置
//    @PostMapping("/document/createByFile")
//    @ApiOperation(value = "在知识库中通过文件创建文档", notes = "在知识库中通过文件创建文档")
//    public ResponseEntity<String> createDocumentByFile(@RequestParam String datasetId, @RequestBody CreateDocumentReq params) {
//        try {
//            String result = difyService.createDocumentByFile(params.getDatasetId(), params.getFileData());
//            return new ResponseEntity<>(result, HttpStatus.OK);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new ResponseEntity<>("通过文件创建文档失败：" + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
//        }
//    }
//
//    @PostMapping("/documents/{documentId}/updateByFile")
//    @ApiOperation(value = "通过文件更新文档", notes = "通过文件更新文档")
//    public ResponseEntity<String> updateDocumentByFile(@PathVariable("documentId") String documentId,
//                                                        @RequestBody CreateDocumentParams params) {
//        try {
//            String result = difyService.updateDocumentByFile(params.getDatasetId(), documentId, params.getFileData());
//            return new ResponseEntity<>(result, HttpStatus.OK);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new ResponseEntity<>("通过文件更新文档失败：" + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
//        }
//    }
}
